<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Registration extends Controller {

    function Registration() {
        parent::Controller();
    }

    function index() {
        $data['page'] = 'registration';
        $data['pagenum'] = 1;
        //data buat form


        $this->load->view('layout/main', $data);
    }

    function flowchart_2() {
        $data['page'] = 'registration';
        $data['pagenum'] = 2;
        $this->load->view('layout/main', $data);
    }

    function formdata(){
        $data['form_details']['username'] = array(
            'id' => 'username',
            'name' => 'username',
            'size' => '31',
            "OnFocus" => "cek_username();",
            "OnClick" => "cek_username();",
            "ONCHANGE" => "cek_username();",
            "onKeydown" => "cek_username();",
            "onKeyup" => "cek_username();"
        );

        $data['form_details']['password'] = array(
            'id' => 'password',
            'name' => 'password',
            'size' => '31',
        );

        $data['form_details']['repassword'] = array(
            'id' => 'repassword',
            'name' => 'repassword',
            'size' => '31',
            "OnFocus" => "cek_password();",
            "OnClick" => "cek_password();",
            "ONCHANGE" => "cek_password();",
            "onKeydown" => "cek_password();",
            "onKeyup" => "cek_password();"
        );

        $data['form_details']['email'] = array(
            'id' => 'email',
            'name' => 'email',
            'size' => '31',
            "OnFocus" => "cek_email();",
            "OnClick" => "cek_email();",
            "ONCHANGE" => "cek_email();",
            "onKeydown" => "cek_email();",
            "onKeyup" => "cek_email();"
        );

        $data['form_details']['university'] = array(
            'id' => 'university',
            'name' => 'university',
            'size' => '31',
        );

        $data['form_details']['country'] = array(
            'id' => 'country',
            'name' => 'country'
        );

        $data['form_details']['zone'] = array(
            'id' => 'zone',
            'name' => 'zone'
        );

        $data['form_details']['province'] = array(
            'id' => 'province',
            'name' => 'province',
            'width' => '300',
        );


        $data['form_details']['team_name'] = array(
            'id' => 'team_name',
            'name' => 'team_name',
            'size' => '45',
            "OnFocus" => "cek_teamname();",
            "OnClick" => "cek_teamname();",
            "ONCHANGE" => "cek_teamname();",
            "onKeydown" => "cek_teamname();",
            "onKeyup" => "cek_teamname();"
        );

//            $data['form_details']['leader_name'] = array(
//                    'id' => 'leader_name',
//                    'name' => 'leader_name'
//            );
//            
//            $data['form_details']['leader_gender'] = array(
//                    'id' => 'leader_gender',
//                    'name' => 'leader_gender'
//            );
//            
//            $data['form_details']['leader_dob'] = array(
//                    'id' => 'leader_dob',
//                    'name' => 'leader_dob'
//            );
//            
//            $data['form_details']['leader_phone'] = array(
//                    'id' => 'leader_phone',
//                    'name' => 'leader_phone'
//            );
        
        $data['form_details']['fail'] = false;
        
        $data['form_details']['member_name_1'] = array(
            'id' => 'member_name_1',
            'name' => 'member_name_1',
            'size' => '31',
            "OnFocus" => "cek_membername_1();",
            "OnClick" => "cek_membername_1();",
            "ONCHANGE" => "cek_membername_1();",
            "onKeydown" => "cek_membername_1();",
            "onKeyup" => "cek_membername_1();"
        );

        $data['form_details']['member_name_2'] = array(
            'id' => 'member_name_2',
            'name' => 'member_name_2',
            'size' => '31',
            "OnFocus" => "cek_membername_2();",
            "OnClick" => "cek_membername_2();",
            "ONCHANGE" => "cek_membername_2();",
            "onKeydown" => "cek_membername_2();",
            "onKeyup" => "cek_membername_2();"
        );

        $data['form_details']['member_name_3'] = array(
            'id' => 'member_name_3',
            'name' => 'member_name_3',
            'size' => '31',
            "OnFocus" => "cek_membername_3();",
            "OnClick" => "cek_membername_3();",
            "ONCHANGE" => "cek_membername_3();",
            "onKeydown" => "cek_membername_3();",
            "onKeyup" => "cek_membername_3();"
        );

        $data['form_details']['member_name_4'] = array(
            'id' => 'member_name_4',
            'name' => 'member_name_4',
            'size' => '31',
            "OnFocus" => "cek_membername_4();",
            "OnClick" => "cek_membername_4();",
            "ONCHANGE" => "cek_membername_4();",
            "onKeydown" => "cek_membername_4();",
            "onKeyup" => "cek_membername_4();"
        );

        $data['form_details']['cv'] = array(
            'id' => 'cv',
            'name' => 'cv'
        );

        $data['form_details']['id_card'] = array(
            'id' => 'id_card',
            'name' => 'id_card'
        );

        $data['form_details']['letter'] = array(
            'id' => 'letter',
            'name' => 'letter'
        );

        $data['form_details']['submit'] = array(
            'id' => 'submit_reg',
            'name' => 'submit_reg',
            'value' => 'Submit Registration'
        );
        return $data;
    }
    
    function form() {
        //$this->load->helper('captcha');
        $data= $this->formdata();

        $data['page'] = 'registration';
        $data['pagenum'] = 3;
        $this->load->view('layout/main', $data);
    }

    function success(){
        //ambil email dari db
        $this->load->model('user_model', 'usermdl', TRUE);
        $user = $this->usermdl->get_user_details($this->input->post('username'));
        
        $data['page'] = 'reg_success';
        $data['url']= base_url() . index_page() . '/registration/verify/' . $this->input->post('username') . '/' . $user['details']['hash_code'];
        $this->load->view('layout/main', $data);
    }
    
    
    function submit() {
        //disini buat submit ke database
        //echo $this->input->post('username');
        //load model buat masukin ke db
        //validasi input dulu
            $this->load->model('register_m', 'regm', TRUE);

            //upload_file
            $config['upload_path'] = './user_files/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_width'] = '2048';
            $config['max_height'] = '1536';
            // $config['file_name'] = $this->input->post('username'). '_id_card';
            $this->load->library('upload', $config);
            
            //echo $config['upload_path'];$this->regm->register_member($is_upload);
            //cek dulu apakah username ada?
            $query = $this->db->query("select * from users where username='". $this->input->post('username'). "'");
            if ($query->num_rows() == 1)
            {
                $data= $this->formdata();

                 $data['page'] = 'registration';
                $data['pagenum'] = 3;
                $data['form_details']['fail'] = true;
                $this->load->view('layout/main', $data);
            }
            else{
                $this->regm->register_member();
                $this->upload_letter();
                $this->upload_IDcard();
                $this->upload_CV();
                $this->regm->validate($this->input->post('username'));
                $this->send_email($this->input->post('username'));
                $this->success();
            }
        //tampilkan halaman sukses : isinya nyuruh cek email
        //$this->load->view('layout/main', $data);
            
    }

    
    function upload_adm(){
//        $this->load->library('ftp');
//        
//        $this->ftp->chmod('./user_files/', 777);
        $config['upload_path'] = './user_files/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_width'] = '2048';
            $config['max_height'] = '1536';
            // $config['file_name'] = $this->input->post('username'). '_id_card';
            $this->load->library('upload', $config);
        
        $this->load->model('user_model','userm', TRUE);
        
        $user = $this->userm->get_user_details($this->session->userdata('username'));
            
        $isl = $user['details']['is_letter'];
        $is_letter = $this->upload_letter_login($isl);
        $is_id = $this->upload_IDcard_login();
        $is_cv = $this->upload_CV_login();
        
        $this->db->where('username', $this->session->userdata('username'));
        $update = array(
            'is_letter' => $is_letter,
            'is_id' => $is_id,
            'is_cv' => $is_cv
        );
        
        $this->db->update('users', $update);
        redirect(base_url(). index_page(). '/profile/home', 'location');
    }
    
    
    function send_email($username) {
        //config email
        //kalo udah di server ITB, confignya kyk yg di user guide CI aja
//        $config['protocol'] = 'smtp';
//        //$config['mailpath'] = '/usr/sbin/sendmail';
//        $config['charset'] = 'iso-8859-1';
//        $config['wordwrap'] = TRUE;
//
//        $config['smtp_host'] = 'ssl://smtp.gmail.com';
//        $config['smtp_timeout'] = '7';
//        $config['smtp_user'] = 'a.giovani17@gmail.com';
//        $config['smtp_pass'] = '';
//        $config['smtp_port'] = 465;
//        $config['mailtype'] = 'text';
        //ini yg dari CI userguide
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'localhost';
        $config['validate'] = 'TRUE';
        
        //ambil email dari db
        $this->load->model('user_model', 'usermdl', TRUE);
        $user = $this->usermdl->get_user_details($username);

        $this->load->library('email');
        $this->email->initialize($config);
        $this->email->set_newline("\r\n");
        $this->email->from('no-reply@iecom.itb.ac.id', 'IECOM 2012');
        $this->email->to($user['details']['email']);

        $this->email->subject('Welcome to IECOM 2012 : The 5th Industrial Engineering Competition');
        $this->email->message('To participate in IECOM 2012, please verify your account. 
                Click this link : ' . 
                base_url() . index_page() . '/registration/verify/' . $username . '/' . $user['details']['hash_code']) .
                '\nPlease do not reply this email\n Thank you for your attention.';

        $this->email->send();
        //echo $this->email->print_debugger();
    }

    function verify($username, $hash_code) {
        $this->load->model('user_model', 'usermdl', TRUE);
        $user = $this->usermdl->get_user_details($username);

        if ($user['details']['hash_code'] == $hash_code) {
            $this->db->where('username', $username);
            $update = array('is_active' => 1, 'hash_code' => null);
            $this->db->update('users', $update);
            redirect(base_url() . index_page(), 'location');
        } else {
            $data['page'] = 'already_active';
            $this->load->view('layout/main', $data);
        }
    }

    function upload_letter() {
        if (!$this->upload->do_upload('letter')) {
            $error = array('error' => $this->upload->display_errors());
            
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_letter' . $data['upload_data']['file_ext']);
            $this->db->where('username', $this->input->post('username'));
            $update = array('is_letter' => 1);
            $this->db->update('users', $update);
        }
    }

    function upload_IDcard() {
        $this->load->model('user_model', 'userm', TRUE);
        //$username = $this->input->post('username');
        $count = 0;
        if (!$this->upload->do_upload('id_card_1')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member1' . '_ID' . $data['upload_data']['file_ext']);
            $count++;
            $this->db->where('username', $this->input->post('username'));
            $update = array('is_id1' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('id_card_2')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member2' . '_ID' . $data['upload_data']['file_ext']);

            $count++;
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_id2' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('id_card_3')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member3' . '_ID' . $data['upload_data']['file_ext']);

            $count++;
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_id3' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('id_card_4')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member4' . '_ID' . $data['upload_data']['file_ext']);

            $count++;
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_id4' => 1);
            $this->db->update('users', $update);
        }

    }

    function upload_CV() {
        
        $this->load->model('user_model', 'userm', TRUE);
         //$username = $this->input->post('username');
        $count = 0;
        if (!$this->upload->do_upload('cv_1')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member1' . '_cv' . $data['upload_data']['file_ext']);

            $count++;
            
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_cv1' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('cv_2')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member2' . '_cv' . $data['upload_data']['file_ext']);

            $count++;
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_cv2' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('cv_3')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member3' . '_cv' . $data['upload_data']['file_ext']);

            $count++;
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_cv3' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('cv_4')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->input->post('username') . '_member4' . '_cv' . $data['upload_data']['file_ext']);

            $count++;
             $this->db->where('username', $this->input->post('username'));
            $update = array('is_cv4' => 1);
            $this->db->update('users', $update);
        }

    }
    
    //buat yg login:
    
    function upload_letter_login($isl) {
        if ($this->upload->do_upload('letter')) {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username'). '_letter' . $data['upload_data']['file_ext']);
            return 1;
        } else if (!$this->upload->do_upload('letter')) {
            if ($isl == 1){
                return 1;
            }
            else{
                $error = array('error' => $this->upload->display_errors());
                return 0;
            }
        }
    }

    function upload_IDcard_login() {
        $this->load->model('user_model', 'userm', TRUE);
       
        
        if (!$this->upload->do_upload('id_card_1')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member1' . '_ID' . $data['upload_data']['file_ext']);
           
            $username = $this->session->userdata('username');
             $this->db->where('username', $username);
            $update = array('is_id1' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('id_card_2')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member2' . '_ID' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_id2' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('id_card_3')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] .$this->session->userdata('username') . '_member3' . '_ID' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_id3' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('id_card_4')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member4' . '_ID' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_id4' => 1);
            $this->db->update('users', $update);
        }
        
         $user = $this->userm->get_user_details($this->session->userdata('username'));
        if ($user['details']['is_id1'] == 1
                && $user['details']['is_id2'] == 1
                && $user['details']['is_id3'] == 1
                && $user['details']['is_id4'] == 1) {
             
            $this->db->where('username', $this->session->userdata('username'));
            $update = array(
                'is_id' => 1
            );

            $this->db->update('users', $update);
            return 1;
        } else {
            return 0;
        }
    }

    function upload_CV_login() {
        $this->load->model('user_model', 'userm', TRUE);
        if (!$this->upload->do_upload('cv_1')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member1' . '_cv' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_cv1' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('cv_2')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member2' . '_cv' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_cv2' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('cv_3')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member3' . '_cv' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_cv3' => 1);
            $this->db->update('users', $update);
        }

        if (!$this->upload->do_upload('cv_4')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());

            
            rename($data['upload_data']['file_path'] . $data['upload_data']['file_name'], $data['upload_data']['file_path'] . $this->session->userdata('username') . '_member4' . '_cv' . $data['upload_data']['file_ext']);

            $username = $this->session->userdata('username');
            $this->db->where('username', $username);
            $update = array('is_cv4' => 1);
            $this->db->update('users', $update);
        }
         $user = $this->userm->get_user_details($this->session->userdata('username'));
        if ($user['details']['is_cv1'] == 1
                && $user['details']['is_cv2'] == 1
                && $user['details']['is_cv3'] == 1
                && $user['details']['is_cv4'] == 1) {
            $this->db->where('username', $this->session->userdata('username'));
            $update = array(
                'is_cv' => 1
            );

            $this->db->update('users', $update);
            return 1;
        } else {
            return 0;
        }
    }
    
    function validate($username, $hash_code) {
        
        //load model
        //cek hash code
        //validasi, ubah is_active jadi 1
    }

}

?>
